#!/usr/bin/env python3
import threading
from multiprocessing import Process
import asyncio
import json
import sys
import signal
import secrets
import time
from websockets.sync.client import connect

user = {
        "api_token":"liqing",
        "type":"init"
    }

def signal_handler(signal, frame):
    sys.exit(0)

def hello(i):
     user['api_token'] = "liqing_{}".format(i+1)
     print(i+1)
     with connect("ws://47.104.233.79:8765") as websocket:
        #user["api_token"] = "liqing_"
        websocket.send(json.dumps(user))
        message =   websocket.recv()
        print(f"Received: {message}")
        event = {
                "type":"heart"
                }
        while True:
            #time.sleep(1)
            #websocket.send(json.dumps(event))
            message =  websocket.recv()
            if message:
                print(f"Received: {message}")
if __name__ == "__main__":
    signal.signal(signal.SIGINT, signal_handler)
    for i in range(1200):
        #t = threading.Thread(target=hello, args=(i,))
        #t.start()
          time.sleep(0.2)
          p = Process(target=hello, args=(i,))
          p.start()

    #hello()
